package com.ituzhi.mqtt.server.service;

import com.ituzhi.mqtt.server.config.MqttInboundConfiguration;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * packageName com.ituzhi.mqtt.server.service
 *
 * @author 死道友不死贫道
 * @version JDK 17
 * @className MqttMessageSubscriber
 * @date 2025-01-23
 * @description TODO
 */
@Service
public class MqttMessageSubscriber {

    @Resource
    private MqttInboundConfiguration mqttInboundConfiguration;


    public String subscribeTopic(String topic) {
        try {
            mqttInboundConfiguration.addSubscription(topic, 1);
            return "Subscribed to topic: " + topic;
        } catch (Exception e) {
            e.printStackTrace();
            return "Failed to subscribe to topic: " + topic;
        }
    }

    public String subscribeTopic(String topic, Integer qos) {
        if (null == qos) {
            qos = 1;
        }
        try {
            mqttInboundConfiguration.addSubscription(topic, qos);
            return "Subscribed to topic: " + topic;
        } catch (Exception e) {
            e.printStackTrace();
            return "Failed to subscribe to topic: " + topic;
        }
    }
}